package net.versiforma.libraryreminder;

import java.util.ArrayList;


public class LibraryAccount {
	
	private String libraryName;
	private boolean isActive = false;
	private String userid;
	private String password;
	private int soonThresholdDays = 7;
	
	private ArrayList<BorrowedItem> borrowedItems = new ArrayList<BorrowedItem>();
	private ArrayList<BorrowedItem> dueSoonItems;
	private boolean hasDueSoonItems = false;
	
	
	public String getLibraryName() {
		return libraryName;
	}
	public void setLibraryName(String libraryName) {
		this.libraryName = libraryName;
	}
	
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public int getSoonThresholdDays() {
		return soonThresholdDays;
	}
	public void setSoonThresholdDays(int soonThresholdDays) {
		this.soonThresholdDays = soonThresholdDays;
	}	
	public void setSoonThresholdDays(String soonThresholdDays) {
		try {
			this.soonThresholdDays = Integer.parseInt(soonThresholdDays);
		} catch (NumberFormatException e) {
			System.out.println("[WARNING] Wrong threshold format, taking default value: " + this.soonThresholdDays);
		}
	}

	public void setActive(Boolean isActive) {
		this.isActive = isActive;
	}	
	public void setActive(String isActive) {
		if (isActive.equals("1") || isActive.equalsIgnoreCase("true"))
			this.isActive = true;
	}
	public boolean isActive() {
		return isActive;
	}

	
		
	public void setBorrowedItems(ArrayList<BorrowedItem> borrowedItems) {
		this.borrowedItems = borrowedItems;
	}
	public ArrayList<BorrowedItem> getBorrowedItems() {
		return borrowedItems;
	}
	

	public ArrayList<BorrowedItem> getDueSoonItems() {
		// If the due soon list is still empty populate it by processing the borrowed items' list
		if (dueSoonItems == null) {
			dueSoonItems = new ArrayList<BorrowedItem>();
			for (BorrowedItem item:borrowedItems) {
				if (item.isDueSoon(soonThresholdDays))
					dueSoonItems.add(item);
			}
		}
		return dueSoonItems;
	}
	
	public boolean hasDueSoonItems() {
		getDueSoonItems();
		if ( dueSoonItems.size() > 0 ) {
			hasDueSoonItems = true;
		}
		return hasDueSoonItems;
	}

}
